// pages/Index/shopActive/redBag/addRedBag.js
const controller=require("../../../controllers/Controller").controller
const controllers=require("../../../controllers/controllerIndex").controller


Page({

  /**
   * 页面的初始数据
   */
  data: {
    price:"",
    num:"",
    minMoney:""
  },


  //获取红包金额
  getPrice(e){
    e.detail.value = e.detail.value.replace(/[^\d.]/g,"");
    e.detail.value = e.detail.value.replace(/\.{2,}/g,"."); 
    e.detail.value = e.detail.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
    e.detail.value = e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
    if(e.detail.value.indexOf(".")< 0 && e.detail.value !=""){
      e.detail.value= parseFloat(e.detail.value);
    }
    this.setData({
      price:e.detail.value
    })
  },

  //获取红包数量
  getNum(e){
    e.detail.value = e.detail.value.replace(/[^\d]/g,"");
    this.setData({
      num:e.detail.value
    })
  },

  //取消
  cancleBtn(){
    wx.navigateBack()
  },

  //获取最低发送金额
  getMinMoney(){
    controllers.getConfig({no:15}).then(res=>{
      this.setData({
        minMoney:res.data.value
      })
    })
  },

  //创建红包订单
  createRedBagOrder(){
    console.log(parseFloat(this.data.price))
    let price=parseFloat(this.data.price)
    if(!price||!this.data.num){
      wx.showToast({
        icon:"none",
        title:"请填入红包信息",
      })
      return
    }
    if(price<this.data.minMoney){
      wx.showToast({
        icon:"none",
        title:"红包金额最低"+this.data.minMoney+"元",
      })
      return
    }else{
      let maxNum=0
      if(parseFloat(price)>parseInt(this.data.minMoney)){
        maxNum=10+(parseInt(price)-parseInt(this.data.minMoney))*10
      }else{
        maxNum=10
      }
      if(parseInt(this.data.num)>maxNum&&parseInt(this.data.num)>10){
        wx.showToast({
          icon:"none",
          title:"发送数量最多为"+maxNum+"个",
        })
        return
      }else if(parseInt(this.data.num)<10){
        wx.showToast({
          icon:"none",
          title:"发送数量最少为10个",
        })
        return
      }
    }



    let value={}
    value.shopId=wx.getStorageSync('shopId')
    value.price=this.data.price
    value.num=this.data.num
    controller.redOrder(value).then(res=>{
      let orderNo = res.data.orderNo
      let orderId = res.data.id
      controllers.weChatPay({
        shopId: wx.getStorageSync('shopId'),
        orderNo: orderNo,
        type: "SP",
        openId: wx.getStorageSync('openId')
      }).then(res => {
        console.log(res);
        
        let data = JSON.parse(res.data);
        console.log(data)
        wx.requestPayment({
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          timeStamp: data.timeStamp,
          signType: data.signType,
          success(res) {
            wx.showToast({
              icon:"none",
              title: '操作成功',
            })
            setTimeout(function(){
              wx.navigateBack()
            },1000)
          },
          fail(res) {
            console.log(res)
          },
          complete(){
          }
        })
      })

      // this.paySuccess(orderId,orderNo)
    })
  },


  //支付成功
  paySuccess(orderId,orderNo){
    let value={}
    value.shopId=wx.getStorageSync('shopId')
    value.accountId=wx.getStorageSync('accountId')
    value.orderId=orderId
    value.orderNo=orderNo
    controller.paySuccess(value).then(res=>{

    })    

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMinMoney()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})